package com.ghostwan.androears.adapter;

import java.util.ArrayList;

import com.ghostwan.androears.AndroEars;
import com.ghostwan.androears.R;
import com.ghostwan.androears.customObject.Album;
import com.ghostwan.androears.customObject.Song;

import android.content.Context;
import android.net.Uri;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AlbumAdapter extends BaseAdapter
{

	private final LayoutInflater mInflater;
	private final int mLayoutRes;
	public ArrayList<Album> mAlbumList;
	public AndroEars mAndroEars;
	protected String TAG = "AlbumAdapter";

	public AlbumAdapter(AndroEars androEars, int textViewResourceId)
	{
		super();
		mAndroEars = androEars;
		mInflater = (LayoutInflater) mAndroEars.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mLayoutRes = textViewResourceId;
		mAlbumList = new ArrayList<Album>();
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{

		View view;
		if (convertView == null)
		{
			view = mInflater.inflate(mLayoutRes, parent, false);
		}
		else
		{
			view = convertView;
		}
		Album album = this.getItem(position);
		LinearLayout addLayout = (LinearLayout) view.findViewById(R.id.addLayout);
		addLayout.setClickable(true);
		addLayout.setTag(new Integer(position));
		
		// addLayout.setFocusable(true);
		addLayout.setBackgroundResource(android.R.drawable.menuitem_background);
		addLayout.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				try
				{
					Album album = mAlbumList.get((Integer) v.getTag());
					
					ArrayList<Song> songList= mAndroEars.getSongList(mAndroEars.mCurrentArtist,album);
					for (Song song : songList)
					{
						mAndroEars.mPlayerService.addSongPlaylist(song);
					}
					mAndroEars.mMusicPlayerButton.setVisibility(View.VISIBLE);
					String s = new String();
					s =  mAndroEars.getString(R.string.adding)+" "+album.mName
					+mAndroEars.getString(R.string.adding_multiple_song_cp);
					Toast.makeText(mAndroEars,s,Toast.LENGTH_LONG).show();  
				}
				catch (RemoteException e)
				{
					Log.e(TAG, e.getMessage());
				}
				
			}
		});
		ImageView albumCover = (ImageView) view.findViewById(R.id.albumCover);

		if (album.mAlbumCover != null)
		{
			albumCover.setImageURI(Uri.parse(album.mAlbumCover));
		}
		else
		{
			albumCover.setImageResource(R.drawable.album_default);
		}

		((TextView) view.findViewById(R.id.albumName)).setText(album.mName);
		if (album.mSongCount == 1)
			((TextView) view.findViewById(R.id.songCount)).setText(album.mSongCount + " song");
		else
			((TextView) view.findViewById(R.id.songCount)).setText(album.mSongCount + " songs");

		// TODO Auto-generated method stub
		return view;
	}

	public void setAlbumList(ArrayList<Album> list)
	{
		mAlbumList = list;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount()
	{
		return mAlbumList.size();
	}

	@Override
	public Album getItem(int position)
	{
		return mAlbumList.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

}
